
import 'package:flutter/material.dart';

class ClickableGestureDetector extends StatelessWidget {
  final Widget child;
  final VoidCallback? onTap;
  final Function(Offset, BuildContext) onLongPress; // 接收点击位置和上下文
  final ValueChanged<bool> onHandle;
  final HitTestBehavior? behavior;

  const ClickableGestureDetector({
    super.key,
    required this.child,
    this.onTap,
    required this.onLongPress,
    required this.onHandle,
    this.behavior = HitTestBehavior.translucent,
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        // 手势封装
        return GestureDetector(
          behavior: behavior,

          // 当用户按下组件时触发。
          onTapDown: (details) => onHandle.call(true),

          // 当用户滑动离开组件时触发。
          onTapCancel: () => onHandle.call(false),

          // 点击结束
          onTapUp: (details) {
            Future.delayed(const Duration(milliseconds: 50), () {
              onHandle.call(false);
              onTap?.call();
            });
          },

          // 长按开始
          onLongPressStart: (details) {
            // 传递点击位置和上下文
            onHandle.call(true); // 可选，根据需要是否在长按开始时改变状态
            onLongPress(details.globalPosition, context);
          },

          // 长按结束
          onLongPressEnd: (details) {
            onHandle.call(false);
            Future.delayed(const Duration(milliseconds: 50), () {
              onHandle.call(false);
            });
          },
          child: child,
        );
      },
    );
  }
}